package cn.com.backstage.controller.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_circle_post;

import cn.com.api.service.ApiU_circle_postService;

import cn.com.util.*;

public class U_circle_postBaseController {

	private static Logger logger = Logger.getLogger(U_circle_postBaseController.class);

	@Autowired
	private ApiU_circle_postService apiU_circle_postService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_circle_post-list")
	public ModelAndView u_circle_post_list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_circle_post_list.jsp");

		List<ApiU_circle_post> u_circle_posts = apiU_circle_postService.findU_circle_posts();
		mv.addObject("u_circle_posts", u_circle_posts);

		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_circle_post-add-pre")
	public ModelAndView u_circle_post_add_pre(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_circle_post_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiU_circle_post u_circle_post = apiU_circle_postService.findU_circle_postById(Integer.valueOf(id));
			mv.addObject("u_circle_post", u_circle_post);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_circle_post-add")
	public void u_circle_post_add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String circle_id = request.getParameter("circle_id");
		String user_id = request.getParameter("user_id");
		String add_time = request.getParameter("add_time");
		String content = request.getParameter("content");
		String is_anonymous = request.getParameter("is_anonymous");
		String is_delete = request.getParameter("is_delete");
		String latitude = request.getParameter("latitude");
		String longitude = request.getParameter("longitude");
		String ext1 = request.getParameter("ext1");
		String ext2 = request.getParameter("ext2");
		String ext3 = request.getParameter("ext3");
		String ext4 = request.getParameter("ext4");
		String ext5 = request.getParameter("ext5");

		Map<String, Object> map = new HashMap<String, Object>();
		// String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiU_circle_post u_circle_post = new ApiU_circle_post();
			/*
			 * u_circle_post.setId(id); u_circle_post.setCircle_id(circle_id);
			 * u_circle_post.setUser_id(user_id);
			 * u_circle_post.setAdd_time(add_time);
			 * u_circle_post.setContent(content);
			 * u_circle_post.setIs_anonymous(is_anonymous);
			 * u_circle_post.setIs_delete(is_delete);
			 * u_circle_post.setLatitude(latitude);
			 * u_circle_post.setLongitude(longitude);
			 * u_circle_post.setExt1(ext1); u_circle_post.setExt2(ext2);
			 * u_circle_post.setExt3(ext3); u_circle_post.setExt4(ext4);
			 * u_circle_post.setExt5(ext5);
			 */
			apiU_circle_postService.insertU_circle_post(u_circle_post);
		}
		// edit
		else {
			ApiU_circle_post u_circle_post = apiU_circle_postService.findU_circle_postById(Integer.valueOf(id));
			/*
			 * u_circle_post.setId(id); u_circle_post.setCircle_id(circle_id);
			 * u_circle_post.setUser_id(user_id);
			 * u_circle_post.setAdd_time(add_time);
			 * u_circle_post.setContent(content);
			 * u_circle_post.setIs_anonymous(is_anonymous);
			 * u_circle_post.setIs_delete(is_delete);
			 * u_circle_post.setLatitude(latitude);
			 * u_circle_post.setLongitude(longitude);
			 * u_circle_post.setExt1(ext1); u_circle_post.setExt2(ext2);
			 * u_circle_post.setExt3(ext3); u_circle_post.setExt4(ext4);
			 * u_circle_post.setExt5(ext5);
			 */
			apiU_circle_postService.updateU_circle_post(u_circle_post);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_circle_post-delete")
	public void u_circle_post_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiU_circle_postService.deleteU_circle_post(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
